/**
 * 账变记录页面的逻辑处理
 */
var balanceLogUI = function() {
    var self = this;
    PageList.call(self);

    /**
     * 存放结果的table页
     */
    self.table = null;

    /**
     * 是手机版本还是PC版本
     */
    self.phone = false;

    // 日期范围
    self.startInput = null;
    self.endInput = null;

    // 账变类型
    self.typeSelect = null;
    self.type = null;

    // 充值类型
    self.channelSelect = null;

    // 状态
    self.stateSelect = null;

    // 用户名
    self.userInput = null;

    // 日期选择
    self.dateRadio = null;

    // 所属的群的群主ID
    self.owner = null;
    self.group = false;

    // 查看按钮
    self.refreshBtn = null;

    // 自定义的行绘制函数
    self.drawLineFunc = null;

    // 自动刷新
    self.autoChecked = null;
    self.autoTime = 8 * 1000;

    // 后台查询还是前台
    self.admin = false;
};
balanceLogUI.prototype = Object.create(PageList.prototype);

balanceLogUI.prototype.init = function() {
    var self = this;
    var f = function() {
        self.query(1);
    };
    if (self.dateRadio) {
        $(self.dateRadio).change(f);
    }

    self.jumpPage = function(page) {
        self.query(page);
    };

    if (self.startInput) self.startInput.datepicker({format: 'yyyy-mm-dd'});
    if (self.endInput) self.endInput.datepicker({format: 'yyyy-mm-dd'});

    if (self.refreshBtn) {
        self.refreshBtn.click(function() {
            if (self.timer) clearTimeout(self.timer);
            f();
        });
    }

    if (self.autoChecked) {
        self.autoChecked.change(function() {
            if (self.autoChecked[0].checked) {
                self.timer = setTimeout(function() {
                    f();
                }, self.autoTime);
            }
            else {
                if (self.timer) clearTimeout(self.timer);
            }
        });
    }
    f();
}

balanceLogUI.prototype.query = function(page) {
    var self = this;
    if (self.timer) {
        clearTimeout(self.timer);
        self.timer = null;
    }

    // 计算时间范围
    var start = null, end = null;
    if (self.startInput) start = self.startInput.val();
    if (self.endInput) end = self.endInput.val();
    if (self.dateRadio) {
        var start = new Date(get_server_time()*1000).Format("yyyy-MM-dd");
        if ($(self.dateRadio + ":checked").val() == "1") start = new Date(get_server_time()*1000 - 24 * 3600 * 1000).Format("yyyy-MM-dd");
        end = start;
    }

    // 用户名（手机号）
    // TODO: 如果是群，需要先根据用户名取得其手机号
    var uid = null;
    if (self.userInput && self.userInput.val().trim()) uid = self.userInput.val().trim();

    // 账变类型
    var type = self.type;
    if (self.typeSelect) {
        type = self.typeSelect.val();
        if (type == '-1') type = null;
    }

    // 充值类型
    var channel = null;
    if (self.channelSelect) {
        channel = self.channelSelect.val();
        if (channel == '-1') channel = null;
    }

    // 状态
    var state = null;
    if (self.stateSelect) {
        state = self.stateSelect.val();
        if (state == '-1') state = null;
    }

    var owner = '';
    if (self.group || self.wx) {
        owner = self.owner;
    }

    // 开始检索数据
    startLoading();
    var data = {
        pageSize: self.getPageSize(),
        pageNum: page || 1,
        owner: owner,
    };
    if (start) data.start = start;
    if (end) data.end = end;
    if (uid) data.uid = uid;
    if (type) data.type = type;
    if (channel) data.channel = channel;
    if (state) data.state = state;
    var f = function(data) {
        stopLoading();
        self.fillResult(data.list, data.sum);
        $("select").selected();
        self.fillPages(data.count, data.number, data.page, data.size);
    };
    if (self.dw)
        CMD.query_dw_log(data, f);
    else if (self.group)
        CMD.gquery_account_log(data, f);
    else if (self.admin)
        CMD.query_account_log(data, f);
    else
        CMD.query_account_log2(data, f);

    if (self.autoChecked) {
        if (self.autoChecked[0].checked) {
            console.log("延迟8s刷新。");
            self.timer = setTimeout(function() {
                self.query(self.currPage);
            }, self.autoTime);
        }
    }
}

balanceLogUI.prototype.fillResult = function(list, sum) {
    var self = this;
    if (!self.table) return;
    self.table.empty();
    self.list = list;
    if (!self.mobile && sum !== undefined && self.admin) {
        var tr = $("<tr></tr>");
        if (self.drawSumLine) {
            self.drawSumLine((sum/100).toFixed(2), tr);
        }
        else {
            tr.append("<td>--</td>");
            tr.append("<td>--</td>");
            tr.append("<td>--</td>");
            tr.append("<td>合计</td>");
            tr.append("<td>" + (sum/100).toFixed(2) + "</td>");
            tr.append("<td>--</td>");
            tr.append("<td>--</td>");
            tr.append("<td>--</td>");
            if (!self.dw)
                tr.append("<td>--</td>");
        }
        self.table.append(tr);
    }
    for (var i in list) {
        var d = list[i];
        var tr = $("<tr></tr>");
        if (!self.drawLineFunc) {
            if (self.mobile)
                tr.append("<td>" + d.start.substring(11));
            else
                tr.append("<td>" + d.start.substring(5));
            tr.append("<td>" + Util.convertLogType(d.type) + "</td>");
            tr.append("<td class='red'>" + (d.amount/100) + "</td>");
            tr.append("<td>" + (d.balance/100) + "</td>");

            var memo = (d.memo || '无');
            if (d.type == 'withdraw') memo = memo + '开户名：' + (context.user.user.name || '');
            tr.append("<td>" + memo + "</td>");

            if (d.state == 0) tr.append("<td>未完成</td>");
            if (d.state == 1) tr.append("<td>完成</td>");
            if (d.state == 2) tr.append("<td>已拒绝</td>");
        }
        else {
            self.drawLineFunc(d, tr);
        }
        self.table.append(tr);
    }

    $("button.state_pass").click(function(e) {
        var id = $(e.currentTarget).attr("value");
        for (var i in self.list) {
            if (self.list[i].id == id) {
                self.currData = self.list[i];
                self.pass();
                break;
            }
        }
    });
    $("button.state_reject").click(function(e) {
        var id = $(e.currentTarget).attr("value");
        for (var i in self.list) {
            if (self.list[i].id == id) {
                self.currData = self.list[i];
                self.reject(self.list[i]);
            }
        }
    });

    if (self.drawEnd) self.drawEnd();
}

balanceLogUI.prototype.reject = function() {
}

window.BalanceLogUI = new balanceLogUI();